﻿#include "VAOneXMLAdaptorMaterialViscoElastic.h"
#include "rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsMaterialsViscoElastic.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "FITKAcousticsPhysics/FITKSpectraDampingLossFactor.h"
#include "FITKAcousticsPhysics/FITKSpectraShearModulus.h"
#include <iostream>

namespace IO
{
    bool VAOneXMLAdaptorMaterialViscoElastic::adaptR()
    {
        // 返回操作是否成功
        return readMaterialViscoElastic();
    }

    bool VAOneXMLAdaptorMaterialViscoElastic::adaptW()
    {
        // 返回操作是否成功
        return writeMaterialViscoElastic();
    }

    bool VAOneXMLAdaptorMaterialViscoElastic::readMaterialViscoElastic()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        auto nameNode = _node->first_node("Name")->first_node();

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "MaterialViscoElastic name is null");
            return false;
        }

        Acoustics::FITKAcousticsMaterialsViscoElastic* viscoElastic = dynamic_cast<Acoustics::FITKAcousticsMaterialsViscoElastic*>(_dataObj);
        if(!viscoElastic)
        {
            return false;
        }

        //数据读取状态 默认为true
        bool b = true;
        // 名称字符串
        QString strName;
        strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            viscoElastic->setDataObjectName(strName);
        }

        // 读取各项属性
        // 每个属性可能存在缺省配置
        QString dsStr = readStrAttribute(_node, "damping_spectrum", &b);
        if(b)
        {
            int id = getSpectraObjectIdByName(dsStr);
            if(id == -1)
            {
                //粘弹性材料中strName的阻尼损耗因子频谱dsStr为空
                consoleMessage(2, QObject::tr("The damping loss factor spectrum %1  of %2 in viscoelastic materials is empty!").arg(dsStr).arg(strName));
            }
            else
            {
                viscoElastic->setDampingSpectrumID(id);
            }
        }

        double dValue = readDoubleAttribute(_node, "density", &b);
        if(b) viscoElastic->setDensity(dValue);

        double prValue = readDoubleAttribute(_node, "poissons_ratio", &b);
        if(b) viscoElastic->setPoissonsRatio(prValue);

        QString smpStr = readStrAttribute(_node, "shear_modulus_spectrum", &b);
        if(b)
        {

            int id = getSpectraObjectIdByName(smpStr);
            if(id == -1)
            {
                //粘弹性材料中strName的剪切模量频谱dsStr为空
                consoleMessage(2, QObject::tr("The shear modulus spectrum %1 of %2 in viscoelastic materials is empty!").arg(dsStr).arg(strName));
            }
            else
            {
                viscoElastic->setShearModulusSpectrumID(id);
            }
        }

        // 读取备注
        auto note_node = _node->first_node("Note");
        if(note_node)
        {
            auto note = note_node->first_node();

            if (note != nullptr)
            {
                QString strNote = readStrNodeValue(note, &b);

                if(b)
                {
                    viscoElastic->setMaterialDescribe(strNote);
                }
            }
        }
        //读取成功
        return true;
    }

    bool VAOneXMLAdaptorMaterialViscoElastic::writeMaterialViscoElastic()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        Acoustics::FITKAcousticsMaterialsViscoElastic* viscoElastic = dynamic_cast<Acoustics::FITKAcousticsMaterialsViscoElastic*>(_dataObj);

        // 判断指针
        if (!viscoElastic)
        {
            return false;
        }

        writeAttribute(_node, "density", viscoElastic->getDensity());
        writeAttribute(_node, "shear_modulus_spectrum", getSpectraObjectNameByID(viscoElastic->getShearModulusSpectrumID()));
        writeAttribute(_node, "damping_spectrum", getSpectraObjectNameByID(viscoElastic->getDampingSpectrumID()));
        writeAttribute(_node, "poissons_ratio", viscoElastic->getPoissonsRatio());
        // 创建名称节点
        createNodeElement(_node, "Name", viscoElastic->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", viscoElastic->getMaterialDescribe());
        //数据写入成功
        return true;
    }
}
